package adminSys

import (
	adminSys "fiberAmisAdmin/controllers/adminSys/sysServer"
	"fiberAmisAdmin/controllers/amis"
	"fmt"
	"github.com/gofiber/fiber/v2"
)

// 定义接口
type UserHandler interface{}

// UserController 结构体
type UserController struct {
	BaseSysController
}

func (uc *UserController) Config() map[string]interface{} {
	return map[string]interface{}{
		"tableName": "admin_users",
		"page":      "1",
		"pageSize":  "5",
	}
}

// buildAttribute 构建属性
func (uc *UserController) buildAttribute(page string, pageSize string) map[string]interface{} {
	return map[string]interface{}{
		"tableName": uc.Config()["tableName"],
		"page":      page,
		"pageSize":  pageSize,
	}
}
func (uc *UserController) List(c *fiber.Ctx) error {
	page := c.Query("page", uc.Config()["page"].(string))
	pageSize := c.Query("perPage", uc.Config()["pageSize"].(string))
	attribute := uc.buildAttribute(page, pageSize)

	fields := []map[string]interface{}{
		{
			"name":  "index",
			"label": "序号",
			"type":  "tpl",
			"tpl":   "${index + 1}",
		},
		{
			"label": "ID",
			"name":  "id",
			"type":  "text",
		}, {
			"label": "用户名",
			"name":  "username",
			"type":  "text",
		}, {
			"label": "时间",
			"name":  "updated_at",
			"type":  "text",
		},
	}

	button := map[string]bool{
		"showCreateButton": true,
		"showEditButton":   true,
		"showDeleteButton": true,
		"showDetailButton": false,
	}
	adminServer := &adminSys.AdminSysUsers{
		UserServer: &adminSys.UserServer{},
	}
	conditions, params := "", ""
	data, err := amis.TableColumn(fields, uc.Form(), uc.detail(), c.Path(), attribute, button, conditions, params, adminServer.UserServer)
	if err != nil {
		fmt.Println("err:", err)
		return uc.returnError(c, fiber.StatusOK, "Error")
	}
	return uc.returnSuccess(c, fiber.StatusOK, "", data)
}

// form 方法定义表单字段
func (uc *UserController) Form() []map[string]interface{} {
	fields := []map[string]interface{}{
		{
			"label":       "用户 ID",
			"name":        "user_id",
			"type":        "input-text",
			"required":    true,
			"placeholder": "请输入用户 ID",
		},
	}
	return fields
}

// detail 方法定义详细信息字段
func (uc *UserController) detail() []map[string]interface{} {
	fields := []map[string]interface{}{
		{
			"label": "用户 ID",
			"name":  "user_id",
			"type":  "text",
		},
	}
	return fields
}
